package com.market.sys.api.client;

import cn.hutool.core.bean.BeanUtil;
import com.market.sys.common.Result;
import com.market.sys.entity.County;
import com.market.sys.entity.Province;
import com.market.sys.entity.Street;
import com.market.sys.service.ICityService;
import com.market.sys.service.ICountyService;
import com.market.sys.service.IProvinceService;
import com.market.sys.service.IStreetService;
import com.market.sys.vo.CountyVO;
import com.market.sys.vo.ProvinceVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 省市表 前端控制器
 * </p>
 *
 * @author xd
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/sys/province")
public class ProvinceController {

    @Resource
    private IProvinceService provinceService;

    @Resource
    private ICityService cityService;

    @Resource
    private ICountyService countyService;

    @Resource
    private IStreetService streetService;

    @PostMapping("/fetchAllProvinceAndCity")
    public Result<?> fetchAllProvinceAndCity(){
        List<Province> provinceList = provinceService.query().list();
        List<ProvinceVO> provinceVOList = new ArrayList<>();
        for (Province province : provinceList) {
            ProvinceVO provinceVO = new ProvinceVO();
            BeanUtil.copyProperties(province, provinceVO, false);
            provinceVO.setCityList(cityService.query().eq("province_id",province.getId()).list());
            provinceVOList.add(provinceVO);
        }
        return Result.success(provinceVOList);
    }

    @PostMapping("/fetchCounty")
    public Result<?> fetchCounty(@RequestBody Map<String,Object> paramMap){
        List<County> countyList = countyService.query().eq("city_id",paramMap.get("cityId")).list();
        List<CountyVO> countyVOList = new ArrayList<>();
        for (County county : countyList) {
            CountyVO countyVO = new CountyVO();
            BeanUtil.copyProperties(county, countyVO, false);
            List<Street> streetList = streetService.query().eq("county_id", county.getId()).list();
            countyVO.setStreetList(streetList);
            countyVOList.add(countyVO);
        }
        return Result.success(countyVOList);
    }
}
